package com.example.dunye.koapp
//import kotlinx.android.synthetic.main.activity_guiding.*
//import android.view.View
//import android.widget.Toast
import android.os.Bundle
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.webkit.WebView
import android.webkit.WebViewClient
import android.webkit.WebChromeClient
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.longToast
//import org.jetbrains.anko.toast
class MainActivity : AppCompatActivity() {
//    bool flag = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val settings = web_view!!.settings
        //设置WebView可以与JS交互 Android端调用Js
        settings.javaScriptEnabled = true
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true)
        }
        settings.javaScriptCanOpenWindowsAutomatically = true // 允许Js中弹窗
        settings.allowFileAccess = true// 设置允许访问文件数据
        settings.setSupportZoom(true)//支持缩放
        settings.domStorageEnabled = true  // 支持web的本地存储
        settings.databaseEnabled = true    // 支持web本地数据库
        settings.allowFileAccessFromFileURLs = true
        //  settings.setAppCacheEnabled(false)
        // 设置2个WebViewClient
        web_view.webViewClient = MyWebViewClient()         //必写
        web_view.webChromeClient = MyWebChromClient()
        web_view.loadUrl("https://www.southmax.com.cn")
        bt1.text = "0"
        bt1.setOnClickListener {
            web_view.evaluateJavascript("javascript:clickFromAndroid()") { value ->
                longToast("Look  "+ value )
            }
//            web_view.loadUrl("javaScript:clickFromAndroid()")
            bt1.text = if(bt1.text=="0") {"1"} else { "0" }
            longToast("xHello Look  "+ bt1.text )
        }
        //        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        //            web_view.settings.allowContentAccess = true
        //        }
        //       设置不用系统浏览器打开,直接显示在当前Webview
        //        web_view.setWebViewClient(object : WebViewClient() {
        //            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
        //                view.loadUrl(url)
        //                return true
        //            }
        //        })

    }

    inner  private class MyWebViewClient: WebViewClient(){
        override fun onPageFinished(view: WebView?, url: String?) {
            //页面加载完成
            super.onPageFinished(view, url)
            //kotlin与H5通信方式2：Kotlin调用H5方法
//            web_view.loadUrl("javaScript:方法名（参数）")
//            val json = JSONObject()
//            json.put("name","Kotlin")
//            web_view.loadUrl("javaScript:clickFromAndroid()")
//            web_view.loadUrl("javaScript:showMessage("+json+")")
        }
    }

    private class MyWebChromClient : WebChromeClient(){
        //加载进度条
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
        }
    }

    /**
     * 重写返回回调监听
     */
    override fun onBackPressed() {
        //判断WebView是否可返回
        if (web_view.canGoBack()) {
            //返回上一个页
            web_view.goBack()
            return
        }
        super.onBackPressed()
    }

}
